package com.xiaomi.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xiaomi.shop.entity.ProductInfo;
import com.xiaomi.shop.entity.ProductType;
import com.xiaomi.shop.entity.ProductVO;
import com.xiaomi.shop.service.ProductInfoService;
import com.xiaomi.shop.service.ProductTypeService;
import com.xiaomi.shop.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName 陈欣月
 * @Descripttion TODO
 * @Author cxy
 * @Date 2021/10/13 19:43
 * @Version 1.0
 **/
@Controller
@RequestMapping("/prod")
public class ProductInfoController {
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private ProductInfoService productInfoService;

   // public int page_size=5;
    public static final int PAGE_SIZE=5;
    //保存图片的文件名
    public static String  saveFileName="";
    @RequestMapping("/split.action")
    public String split(Integer page, ProductVO productVO, Model model){
        //所有分类
        List<ProductType> ptlist  = productTypeService.findAll();
        //放页面上
        model.addAttribute("ptlist",ptlist);
        //声明一个变量
        Integer p=1;
        if(page!=null){
            p = page;
        }
        if(page==null){
            //如果为空就是第一页
            page=p;
        }
        //分页了
        //用于多条件组合查询
       // PageInfo pageInfo = productInfoService.selectConditionSplitPage(productVO,page_size);
        PageInfo pageInfo = productInfoService.splitPage(page, PAGE_SIZE);

        model.addAttribute("pb",pageInfo);

        return "product";
    }

    @RequestMapping("/ajaxSplit.action")
    public String ajaxSplit(ProductVO productVO, HttpSession session,Integer page,Model model){
        //所有的分类
        List<ProductType> ptlist = productTypeService.findAll();
        //放到页面上
        model.addAttribute("ptlist",ptlist);
        //声明一个变量
        Integer p=1;
        if(page!=null){
            p = page;
        }
        if(page==null){
            //如果为空就是第一页
            page=p;
        }
        productVO.setPage(page);
        //获取pageinfo
        PageInfo<ProductInfo> pageInfo = productInfoService.selectConditionSplitPage(productVO, PAGE_SIZE);
        //存session
        session.setAttribute("productVO",productVO);

        model.addAttribute("pb",pageInfo);

        return "product";
    }
    @RequestMapping("/ajaxSplit2.action")
    public String ajaxSplit2(HttpSession session,Integer page,Model model){
        //所有的分类
        List<ProductType> ptlist = productTypeService.findAll();
        //放到页面上
        model.addAttribute("ptlist",ptlist);
        //声明一个变量
        Integer p=1;
        if(page!=null){
            p = page;
        }
        if(page==null){
            //如果为空就是第一页
            page=p;
        }
        ProductVO productVO = (ProductVO)session.getAttribute("productVO");
        productVO.setPage(page);
        //获取pageinfo
        PageInfo<ProductInfo> pageInfo = productInfoService.selectConditionSplitPage(productVO, PAGE_SIZE);

        model.addAttribute("pb",pageInfo);

        return "product";
    }

    @RequestMapping("/delete.action")
    public String delete(Integer pid){
        productInfoService.deleteByPrimaryKey(pid);
       // return "product";
        return "redirect:/prod/ajaxSplit2.action";
    }

    /**
     *  ajax上传图片
     * @param myfile 上传的图片
     * @param request  请求对象
     * @return  某个页面
     */
    @ResponseBody
    @RequestMapping("/ajaxImg.action")
    public String ajaxImg(MultipartFile myfile, HttpServletRequest request){
        //经过测试 发现zhuhe 变了
        saveFileName = FileUtils.getUUIDfileName()+FileUtils.getFileType(myfile.getOriginalFilename());
        //定义一个路径
        String path ="";

        try {
            path = request.getServletContext().getRealPath("/image_big");
            myfile.transferTo(new File(path+File.separator+saveFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = new JSONObject();
        //添加属性
        jsonObject.put("imgurl",request.getContextPath()+"/image_big"+saveFileName);//图片地址的url
        jsonObject.put("imgName",saveFileName);//保存图片的名字

        System.out.println("=============>>>"+jsonObject.toString());
        return  jsonObject.toString();
    }

    @RequestMapping("/save.action")
    public String save(ProductInfo productInfo,Model model){
       // productInfoService.insertSelective();
        productInfo.setpImage(saveFileName);
        productInfoService.save(productInfo);
        //所有的分类
        List<ProductType> ptlist = productTypeService.findAll();
        //放页面上
        model.addAttribute("ptlist",ptlist);
        return "redirect:/prod/split.action";
    }
    @RequestMapping("/updata.action")
    public String updata(Integer pid){
        // productInfoService.insertSelective();

        return "redirect:/prod/split.action";
    }
}
